﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XMM.Dialoges.Library
{
    /// <summary>
    /// Interaktionslogik für FileEditor.xaml
    /// </summary>
    public partial class FileEditor : Window
    {
        /// <summary>
        /// List of currently selected files.
        /// </summary>
        public List<string> SelectedFiles { get; set; }

        /// <summary>
        /// Reference to parent Holster_Files()-Control.
        /// </summary>
        private Controls.Library.Holster_Files ParentFilesHolster { get; set; }

        /// <summary>
        /// Initialize a new FileEditor()-Dialog.
        /// </summary>
        /// <param name="ctlParentFileHolster">Reference to parent Holster_Files()-Control.</param>
        /// <param name="lSelectedFiles">List of selected files.</param>
        public FileEditor(Controls.Library.Holster_Files ctlParentFileHolster, List<string> lSelectedFiles)
        {
            InitializeComponent();

            ParentFilesHolster = ctlParentFileHolster;
            SelectedFiles = lSelectedFiles;
        }

        /// <summary>
        /// Initialize GUI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (string sFilePath in SelectedFiles)
            {
                lbFiles.Items.Add(sFilePath);
            }
        }

        /// <summary>
        /// Remove file from list.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbRemoveFile_Click(object sender, RoutedEventArgs e)
        {
            if(lbFiles.SelectedItem != null && lbFiles.SelectedItem.ToString() != String.Empty)
            {
                if (lbFiles.Items.Count > 1)
                {
                    SelectedFiles.Remove((string)lbFiles.SelectedItem);
                    lbFiles.Items.RemoveAt(lbFiles.SelectedIndex);
                }
                else
                {
                    MessageBox.Show("Die letzte Datei kann nicht gelöscht werden!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("Bitte wählen Sie zuerst eine Datei aus der Liste!", "Keine Datei ausgewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        /// <summary>
        /// Use OpenFileDialog to add additional Files.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbAddFile_Click(object sender, RoutedEventArgs e)
        {
            string sMediaFileDirectory = String.Empty;

            if(SelectedFiles.Count > 0)
            {
                sMediaFileDirectory = SelectedFiles[0].Substring(0, (SelectedFiles[0].LastIndexOf('\\') - 1));
            }

            System.Windows.Forms.OpenFileDialog dlgOpenFile = new System.Windows.Forms.OpenFileDialog();

            try
            {
                if (sMediaFileDirectory != String.Empty)
                {
                    dlgOpenFile.InitialDirectory = sMediaFileDirectory;
                }

                dlgOpenFile.Filter = "Alle Dateien|*.*";
                dlgOpenFile.Multiselect = true;

                if (dlgOpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (System.IO.File.Exists(dlgOpenFile.FileName))
                    {
                        foreach (string sNewFile in dlgOpenFile.FileNames)
                        {
                            bool bDublicate = false;

                            foreach (string sSelectedFile in SelectedFiles)
                            {
                                if (sSelectedFile == sNewFile)
                                {
                                    bDublicate = true;
                                }
                            }

                            if (bDublicate == false)
                            {
                                SelectedFiles.Add(sNewFile);
                                lbFiles.Items.Add(sNewFile);
                            }
                            else
                            {
                                MessageBox.Show("Die Datei \"" + sNewFile + "\" wurde bereits zu diesem Eintrag hinzugefügt!", "Dublikat erkannt", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Die angegebene Datei \"" + dlgOpenFile.FileName + "\" existiert nicht!", "Datei nicht gefunden", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Hinzufügen von Dateien: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// Submit changes to parent Holster_Files()-Control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbDone_Click(object sender, RoutedEventArgs e)
        {
            ParentFilesHolster.SelectedFiles = SelectedFiles;
            ParentFilesHolster.setFileCount();
            ParentFilesHolster.setStatusIcon();
            Close();
        }
    }
}
